package com.weijian.dal.entity;

import com.baomidou.mybatisplus.annotation.*;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.NoArgsConstructor;

import java.time.LocalDateTime;

/**
 * 用户学习统计实体类
 * 对应study_statistics表
 */
@Data
@EqualsAndHashCode(callSuper = false)
@TableName("study_statistics")
@NoArgsConstructor
@AllArgsConstructor
public class StudyStatistics {
    
    @TableId(value = "id", type = IdType.AUTO)
    private Long id;
    
    /**
     * 用户ID
     */
    @TableField("user_id")
    private Long userId;
    
    /**
     * 总学习课程数
     */
    @TableField("total_courses")
    private Integer totalCourses;
    
    /**
     * 已完成课程数
     */
    @TableField("completed_courses")
    private Integer completedCourses;
    
    /**
     * 总学习时长(分钟)
     */
    @TableField("total_study_duration")
    private Long totalStudyDuration;
    
    /**
     * 最后学习时间
     */
    @TableField("last_study_time")
    private LocalDateTime lastStudyTime;
    
    /**
     * 创建时间
     */
    @TableField(value = "created_time", fill = FieldFill.INSERT)
    private LocalDateTime createdTime;
    
    /**
     * 更新时间
     */
    @TableField(value = "updated_time", fill = FieldFill.INSERT_UPDATE)
    private LocalDateTime updatedTime;
    
    /**
     * 增加学习时长
     */
    public void addStudyDuration(long duration) {
        this.totalStudyDuration += duration;
        this.lastStudyTime = LocalDateTime.now();
    }
    
    /**
     * 增加课程数
     */
    public void incrementTotalCourses() {
        this.totalCourses += 1;
    }
    
    /**
     * 增加完成课程数
     */
    public void incrementCompletedCourses() {
        this.completedCourses += 1;
    }
    
    /**
     * 获取完成率
     */
    public double getCompletionRate() {
        if (totalCourses == 0) {
            return 0.0;
        }
        return (double) completedCourses / totalCourses * 100;
    }
}